<?php
    require_ONCE("global.php");
    require_ONCE("HttpApi.php");
    
    if (!isset($_SESSION['user']))
    {
        header("Location:login.php");
        exit();
    }
    
    $userid = $_POST["ownerid"];
    $parentid = $_POST["parentid"];
    $name = $_POST["name"];
    $newname = $_POST["newname"];
    $type = $_POST["type"];
    $path = $_POST["path"];
    
    if ($type === "folder")
    {
        $cmd = "rename_dir";
    }
    else
    {
        $cmd = "rename_file";
    }
    $url = 'http://'.$DS_HOST.'/'.$userid.'/'.$parentid.'/'.$cmd.'?name='.$name.'&newname='.$newname;
    
    //echo $url;
    $http = new HttpApi();
    $params = Array(
        //"name" => $name,
    );
    
    $result = json_decode($http->makeRequest($url, $params));
    $result = get_object_vars($result);
    if ($result["result"] === 0)
    {
        echo '{"fileId":"$name","result":"ok"}';
        $parentname = getdirname($userid, $parentid);
        oplog($userid, $_SESSION['user']["id"], $_SESSION['user']["name"], "browser", $parentname, "web", $name, $newname, "rename ".$type." ".$name. " to ".$newname." in directory ".$parentname);
    }
    else
    {
        echo '{"fileId":"$name","result":"clientBadParams"}';
    }
?>
